// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.specialist.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.AosException;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.enums.FileBusinessTypeEnum;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.ExpertProject;
import com.liuxinlong.modules.out.dto.ProjectDto;
import com.liuxinlong.modules.out.service.OutEcnService;
import com.liuxinlong.modules.out.service.OutProjectService;
import com.liuxinlong.modules.specialist.controller.request.QueryProjectReq;
import com.liuxinlong.modules.specialist.controller.request.UpdateProjectMemberReq;
import com.liuxinlong.modules.specialist.controller.request.UpdateProjectReq;
import com.liuxinlong.modules.specialist.service.SpecialistProjectService;
import com.liuxinlong.modules.system.service.FileManageService;
import com.liuxinlong.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目信息管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023年3月8日
 */
@RestController
@RequestMapping("/specialist-project")
public class SpecialistProjectController {
    @Autowired
    private SpecialistProjectService specialistProjectService;

    @Autowired
    private OutProjectService outProjectService;

    @Autowired
    private OutEcnService outEcnService;

    @Autowired
    private FileManageService fileManageService;

    /**
     * 分页查询项目信息
     *
     * @param req 请求信息
     * @return 项目信息列表
     */
    @PostMapping("page")
    public BaseResp pageProjectInfo(@RequestBody QueryProjectReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("projectManager", req.getProjectManager());
        queryMap.put("projectLevel", req.getProjectLevel());
        queryMap.put("status", req.getStatus());
        queryMap.put("expert", req.getExpert());
        queryMap.put("department", req.getDepartment());
        queryMap.put("expertGroup", req.getExpertGroup());
        queryMap.put("startDate", req.getStartDate());
        queryMap.put("endDate", req.getEndDate());
        List<Map<String, Object>> dataList = specialistProjectService.pageProjectInfo(queryMap);
        Map<String, Object> dataMap = specialistProjectService.queryProjectSummary();
        int totalNum = specialistProjectService.getProjectCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询项目信息成功");
        resp.setDataMap(dataMap);
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 查询目标项目信息
     *
     * @param req 请求信息
     * @return 项目信息列表
     */
    @PostMapping("out-get")
    public BaseResp getProjectInfo(@RequestBody QueryProjectReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", 0);
        queryMap.put("pageSize", 1000);
        queryMap.put("projectSn", req.getProjectSn());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("projectManager", req.getProjectManager());
        List<Map<String, Object>> dataHTPList = outProjectService.queryProjectList(queryMap);
        List<Map<String, Object>> dataECNList = outEcnService.queryProjectList(queryMap);
        List<Map<String, Object>> dataList = new ArrayList<>();
        dataList.addAll(dataHTPList);
        dataList.addAll(dataECNList);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询目标项目信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 新增项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增项目信息", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addProjectInfo(@RequestBody UpdateProjectReq req) {
        ExpertProject project = new ExpertProject();
        String projectSn = req.getId();
        if (StringUtils.isEmpty(projectSn)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "新增失败，项目号为空！");
        }
        project.setProjectSn(projectSn);
        project.setProjectMember(req.getProjectMember());
        project.setAppointedExpert(req.getAppointedExpert());
        project.setDivideExpert(req.getDivideExpert());
        project.setDepartment(req.getDepartment());
        project.setProjectManager(req.getProjectManager());
        project.setStatus(req.getStatus());

        ProjectDto outProject = outProjectService.queryProjectInfo(projectSn);
        ProjectDto outProjectEcn = outEcnService.queryProjectInfo(projectSn);
        if (ObjectUtils.isEmpty(outProject) && ObjectUtils.isEmpty(outProjectEcn)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "新增失败，项目号不存在！");
        }
        Map<String, Object> resultMap = specialistProjectService.addProjectInfo(project,  ObjectUtils.isEmpty(outProject) ? outProjectEcn : outProject);
        BaseResp resp = new BaseResp();
        if (ObjectUtils.isEmpty(resultMap)) {
            resp.setRetMessage("新增项目信息成功");
        } else {
            resp.setRetCode(ReturnCodes.HTTP_IMPORT_PART_FAIL.getRetCode());
            resp.setRetMessage("新增项目信息失败，请校验数据！");
            resp.setDataMap(resultMap);
        }
        return resp;

    }

    /**
     * 修改项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "更新项目信息", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectInfo(@RequestBody UpdateProjectReq req) {
        ExpertProject project = new ExpertProject();
        project.setId(req.getId());
//        project.setProjectPermission(req.getProjectMember());
        project.setProjectMember(req.getProjectMember());
        project.setAppointedExpert(req.getAppointedExpert());
        project.setDivideExpert(req.getDivideExpert());
        project.setStatus(req.getStatus());
        project.setEndDate(req.getEndDate());

        specialistProjectService.updateProjectInfo(project);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改项目信息成功");
        return resp;
    }

    /**
     * 删除项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除项目信息", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteProjectInfo(@RequestBody BaseDeleteReq req) {
        specialistProjectService.deleteProjectInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除项目信息成功");
        return resp;
    }

    /**
     * 导出项目信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出项目信息", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportProjectInfo(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        if (StringUtils.isNotEmpty(req.getId())) {
            specialistProjectService.exportProjectInfo(req.getId(), response);
        } else {
            specialistProjectService.exportProjectList(response);
        }
    }

    /**
     * 查询项目成员信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("member")
    public BaseResp queryMemberInfos(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = specialistProjectService.queryMemberInfos(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目成员信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 查询项目详情信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("detail")
    public BaseResp queryProjectDetail(@RequestBody BaseDeleteReq req) {
        Map<String, Object> dataMap = specialistProjectService.queryProjectDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目详情信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 修改项目成员信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("member-update")
    @LogAnnotation(value = "更新项目成员信息", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectMemberInfo(@RequestBody UpdateProjectMemberReq req) {
        specialistProjectService.updateProjectMemberInfo(req.getProjectId(), req.getProjectMember(), req.getOperateType());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("更新项目成员信息成功");
        return resp;
    }

    /**
     * 查询项目文件信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-query")
    public BaseResp queryFileInfos(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("businessId", req.getId());
        queryParam.put("businessType", FileBusinessTypeEnum.PROJECT_FILE.getValue());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = fileManageService.queryFileList(queryParam);
        int totalNum = fileManageService.getFileCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目文件信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 上传项目文件
     *
     * @param multipartRequest 请求信息
     * @return 成功标识
     */
    @PostMapping("file-add")
    @LogAnnotation(value = "上传项目文件", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_IMPORT)
    public BaseResp addFileInfos(MultipartRequest multipartRequest) {
        specialistProjectService.addFileInfos(multipartRequest);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("上传项目文件成功");
        return resp;
    }

    /**
     * 删除项目文件
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-delete")
    @LogAnnotation(value = "删除项目文件", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteProjectFile(@RequestBody BaseDeleteReq req) {
        fileManageService.deleteFileManage(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除项目文件成功");
        return resp;
    }

    /**
     * 下载项目文件
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-download")
    @LogAnnotation(value = "下载项目文件", business = LogBusinessConstants.EXPERT_PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void downloadProjectFile(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        specialistProjectService.downloadProjectFile(req.getId(), response);
    }

    /**
     * 查询项目专家信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("expert")
    public BaseResp queryExpertInfos(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = specialistProjectService.queryExpertInfos(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目专家信息成功");
        resp.setDataList(dataList);
        return resp;
    }
}
